//
//  LDRegisterVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/6/28.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class LDRegisterVC: LDLoginBaseVC {

    override func viewDidLoad() {
        
        super.viewDidLoad()
    }
    
    //布局
    override func setupDefaultContent() {
        super.setupDefaultContent()
        
        enterBtn.setTitle("同意协议并注册", forState: .Normal)
        phoneTextField.placeholder = "请输入11位的有效手机号码"
        passwordTextField.placeholder = "请输入6位以上的密码"
        
        
        let registerProtocolLab = UILabel.setDetailsLab("注册协议", fontSize: 16, titleColor: LDFontColor.GreenLight, Alignment: .Center)
        
        registerProtocolLab.userInteractionEnabled = true
        registerProtocolLab.bk_whenTapped { [weak self] in
            
            let vc = DDRegisterAgreementViewController()
            self?.navigationController?.pushViewController(vc, animated: true)
        }
        
        view.addSubview(registerProtocolLab)
        
        registerProtocolLab.snp_makeConstraints { (make) in
            make.top.equalTo(enterBtn.snp_bottom).offset(25)
            make.height.equalTo(16)
            make.centerX.equalTo(enterBtn)
        }
        
    }
    
    //按钮操作
    override func changStatus(btn: UIButton) {
        
        if btn.tag == passwordtag {
            
            passwordTextField.secureTextEntry = !passwordTextField.secureTextEntry
            
        } else if btn.tag == securitytag {//获取验证码
            getcodeBtn.userInteractionEnabled = false
            
            requestCodeNet()
        }
    }
    
    //登录
    override func enterClick() {
        
        let phone = phoneTextField.text ?? ""
        let password = passwordTextField.text ?? ""
        let code = securityCodeTextField.text ?? ""
        
        let params = ["mobile":phone,"password":password,"code":code]
        
        LDCheckPublicInfo.shareInstance.registerUserInfo(params, succ: { [weak self] (value) in
            
            // 登录成功
            guard let hspassword = value as? String else {
                
                LDShowHUB.shareInstance.showBarStatus("注册失败")
                return
            }
            
            DDLoginUserManager.setAccount(self?.phoneTextField.text)
            DDLoginUserManager.setToken(hspassword)
            
            let agreement = DDChoosePositonViewController()
            self?.navigationController?.pushViewController(agreement, animated: true)
            
        }) { (data) in
            
            let notice = data as? String ?? "注册失败"
            LDShowHUB.shareInstance.showBarStatus(notice)
        }
    }
    
    override func keyboardHidden(noti: NSNotification?) {
        super.keyboardHidden(noti)
        navigationItem.title = ""
        
    }
    
    override func keyboardShow(noti: NSNotification) {
        super.keyboardShow(noti)
        navigationItem.title = "注册"
    }

}
